﻿using System;

namespace ProjectEuler
{
    internal class Problem12
    {
        private int _currTriNum;
        private int _currTriNumIndex = 1;

        public void Solve()
        {
            var factorCount = 0;
            var triNum = 0;
            while (factorCount <= 500)
            {
                triNum = NextTriNum();
                factorCount = FactorCount(triNum);
            }

            Console.WriteLine("problem 12: " + triNum);
        }

        int FactorCount(int num)
        {
            if (num == 1) return 1;

            int count = 2; // 1 & num are factors, dont test them
            int maxit = num;
            
            for (var i = 2; i < maxit; i++)
            {
                if (num % i == 0)
                {
                    count += 2;     // i and (num/i) are factors
                    maxit = num/i;
                }
            }

            Console.WriteLine("num " + num + " factors " + count);
            return count;
        }

        int NextTriNum()
        {
            _currTriNum += _currTriNumIndex++;
            return _currTriNum;
        }
    }
}